package com.dengyuanke.admin.controller;


import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.dengyuanke.common.result.JsonResult;
import com.dengyuanke.entity.Hospital;
import com.dengyuanke.service.IHospitalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dengyuanke
 * @since 2017-11-13
 */
@Api(tags = "医院相关API")
@RestController
public class HospitalController {

    @Autowired
    private IHospitalService hospitalService;

    /**
     * 添加医院
     * @param hospital
     * @return
     */
    @ApiOperation(value = "添加医院",notes = "传入医院实体")
    @PostMapping("/addhospital")
    public JsonResult addHospital(Hospital hospital){
        hospital.setId(IdWorker.get32UUID());
        hospital.setCreateTime(new Date());
        boolean insert = hospitalService.insert(hospital);
        if(insert){
            return JsonResult.renderSuccess("添加成功");
        }else {
            return JsonResult.renderFail("添加失败");
        }
    }

    /**
     * 更新医院
     * @param hospital
     * @return
     */
    @ApiOperation(value = "更新医院",notes = "传入带id医院实体")
    @PostMapping("/updatehospital")
    public JsonResult updateHospital(Hospital hospital){
        if(StringUtils.isEmpty(hospital.getId())){
            return JsonResult.renderFail("更新医院失败，未传入实体id");
        }
        hospital.setUpdateTime(new Date());
        boolean insert = hospitalService.updateById(hospital);
        if(insert){
            return JsonResult.renderSuccess("更新成功");
        }else {
            return JsonResult.renderFail("更新失败");
        }
    }

    @ApiOperation(value = "获取所有医院",notes = "不需要传入参数")
    @PostMapping("/gethospitals")
    public JsonResult getHospitals(){
        return JsonResult.renderSuccess(hospitalService.selectList(null));
    }


}
